<%@page import="javax.xml.ws.Response"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
	    <%@ page import="org.hibernate.SessionFactory" %>
    <%@ page import="org.hibernate.Session" %>
    <%@ page import="java.util.List" %>
    <%@ page import="hibernate.model.*"  %>
    <%@ page import="hibernate.util.HibernateUtil" %>
    <%@ page import="hibernate.method.*" %>
    <%
        SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
Session s = sessionFactory.openSession();
s.beginTransaction();
 UserMethods user_m = new UserMethods();
 
         Integer user_id = (Integer) session.getAttribute("user_id");
			response.setContentType("text/html");
			if (user_id != null) {
			User u = user_m.id_to_user(sessionFactory,s,user_id);
			List <String> used_emails = user_m.used_emails(sessionFactory, s, u.getType());%>
        
        <form action="EditUser" method="POST" name='formularz'>
		<h3>Edytuj swoje dane</h3>
		<table>
			<tr>
				<td>Nick:</td>
				<td><%out.println(u.getUsername()); %></td>
				<td></td>
			</tr>
			<tr>

				<td>Data urodzenia:</td>
				<td><input
					type="date" id="birth_date" name="birth_date"
					placeholder="<%out.println(u.getBirthdate()); %>"
					pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))"
					onblur="validate('username', this.value)">
				</td>
				<td></td>
			</tr>

			<tr>

				<td>Podaj email:</td>
				<td><input type="email" name="email"
					placeholder="<%out.println(u.getEmail()); %>" maxlength="255"></td>
				<td></td>
			</tr>

			<tr>
				<td>Hasło:</td>
				<td><input type="password" id="password" name="password" required
					 maxlength="80"></td>
				<td></td>
			</tr>
			<tr>
				<td>Powtórz haslo:</td>
				<td><input type="password" id="confirm_password" required
					name="confirm_password" 
					maxlength="80"></td>
				<td></td>
			</tr>
			<tr>
				<td>Avatar:</td>
				<td><input type="file" name="img" multiple></td>
				<td></td>
			</tr>
			<tr>
				<td><input type="hidden" id="type" name="type" value="client" />
				</td>
			</tr>
			<tr>
				<td>Płec:</td>
				<td><select id="gender" name="gender">
	<%			if(u.getGender() == "male") { %>
						<option value="m" selected>męzczyzna</option>
						<option value="f">kobieta</option>
				
				<%}
	else {%>
	<option value="male">męzczyzna</option>
						<option value="female" selected>kobieta</option>
						<% } %>
	</select></td>
				<td></td>
			</tr>
			<tr>
				<td>Imię:</td>
				<td><input type="text" name="name" id="name"
					placeholder="<%out.println(u.getName()); %>" maxlength="255"></td>
				<td> </td>
			</tr>
			<tr>
				<td>Nazwisko:</td>
				<td><input type="text" name="surname" id="surname"
					placeholder="<%
					String surname = u.getSurname();
				if(surname !=null) out.println(surname); %>" maxlength="255"></td>
				<td></td>
			</tr>
			<tr>
				<td>Numer telefonu:</td>
				<td><input type="tel" name="phone" id="phone"
					placeholder="<%
					String phone = u.getPhone(); 
					if(phone != null) out.println(phone);%>" maxlength="80"
					pattern="\d{3}[\-]\d{3}[\-]\d{3}"></td>
				<td></td>
			</tr>
		</table>
		<input type="submit" value="Zmień dane">
		<a href="index.jsp"> Wróc do panelu </a>
	</form>
        <% } else 
        	response.sendRedirect("index.jsp");

 s.getTransaction().commit();
 s.close();
 %>